<template>
  <div class="container">
    <header>
      <van-nav-bar
        title="新增工程验收节点"
        left-arrow
        @click-left="$router.back()">
      </van-nav-bar>
    </header>
    <div class="wd-scroll-body2">
      <div class="wd-group">
        <div class="wd-group__title">基本信息</div>
        <div class="wd-group__body">
          <van-field
            v-model="form.userName"
            label="验收人"
            label-width="130px"
            disabled
          />
          <van-field
            v-model="form.reivewDate"
            @mousedown="ysrqShow = true"
            label="验收日期"
            label-width="130px"
            placeholder="选择验收日期"
            right-icon="clear"
            readonly
            @click-right-icon="form.reivewDate = ''"
          />
          <van-field
            v-model="form.projectName"
            @mousedown="ysxmShow = true"
            label-width="130px"
            label="验收项目"
            placeholder="选择验收项目"
            readonly
          />
          <van-field
            v-model="form.contractName"
            @mousedown="htcxMousedown"
            label-width="130px"
            label="合同名称"
            placeholder="选择合同"
            readonly
          />
          <van-field
            v-model="form.contractCode"
            label-width="130px"
            label="合同编号"
            disabled
          />
          <van-field
            label-width="130px"
            label="责任合同单位"
            v-model="form.contractUnit"
            placeholder="选择责任合同单位"
            @mousedown="zrhtdwMousedown"
            readonly
          />
          <van-field
            v-model="form.payNodeName"
            @mousedown="zfjdMousedown"
            label-width="130px"
            label="支付节点"
            placeholder="选择支付节点"
            readonly
          />
          <van-field
            v-model="form.buildingNames"
            label-width="130px"
            label="楼栋"
            disabled
          />
          <van-field
            v-model="form.subName"
            label-width="130px"
            label="分包合同种类"
            placeholder="选择分包合同种类"
            @mousedown="htzlMousedown"
            readonly
          />
          <van-field
            v-model="form.reviewNode"
            label-width="130px"
            label="形象进度控制节点"
            placeholder="选择节点"
            @mousedown="xxjdMousedown"
            readonly
          />
        </div>
      </div>
    </div>
    <van-row class="wd-pdt10">
      <van-col span="18" offset="3">
        <van-button @click="submitForm" round block type="info">保存</van-button>
      </van-col>
    </van-row>
    <!-- 验收日期选择 -->
    <van-popup
      v-model="ysrqShow"
      position="bottom">
      <van-datetime-picker
        v-model="ysrqValue"
        type="date"
        @cancel="ysrqShow = false"
        @confirm="ysrqConfirm"
      />
    </van-popup>
    <!-- 验收项目查询 -->
    <wd-search-popup
      ref="ysxmSearchPopup"
      :visible.sync="ysxmShow"
      placeholder="输入项目编号或名称查询"
      @search="ysxmSearch"
      @confirm="ysxmConfirm"
    />
    <!-- 合同查询 -->
    <wd-search-popup
      ref="htcxSearchPopup"
      :visible.sync="htcxShow"
      placeholder="输入合同名称查询"
      @search="htcxSearch"
      @confirm="htcxConfirm"
    />
    <!-- 支付节点查询 -->
    <wd-search-popup
      ref="zfjdSearchPopup"
      :visible.sync="zfjdShow"
      placeholder="输入节点名称查询"
      @search="zfjdSearch"
      @confirm="zfjdConfirm"
    />
    <!-- 责任合同选择 -->
    <van-action-sheet 
      v-model="zrhtdw.show"
      :actions="zrhtdw.options"
      @select="zrhtdwSelect"
    />
    <!-- 分包合同种类选择 -->
    <van-action-sheet 
      v-model="htzl.show"
      :actions="htzl.options"
      @select="htzlSelect"
    />
    <!-- 形象进度节点选择 -->
    <van-action-sheet 
      v-model="xxjd.show"
      :actions="xxjd.options"
      @select="mXxjdSelect"
    />
  </div>
</template>
<script>
  import searchPopup from '@/components/searchPopup.vue'
  import moment from 'moment'
  import {
    apiGetUserProject,
    apiGetPayNodeList,
    apiGetPMContractInfo,
    apiGetBuilingList,
    apiGetSubpackageList,
    apiPostBaseInfo,
    apiGetContractCompanyList
  } from '@/api/'

  const userNo = localStorage.getItem('userID')
  const userName = localStorage.getItem('userName')

  export default {
    components: {
      'wdSearchPopup': searchPopup
    },
    data() {
      return {
        htcxShow: false,
        ysxmShow: false,
        ysrqShow: false,
        zfjdShow: false,
        projectCompany: '',
        zrhtdw: {
          show: false,
          options: []
        },
        htzl: {
          show: false,
          options: []
        },
        xxjd: {
          show: false,
          options: []
        },
        ysrqValue: new Date(),
        form: {
          userName: userName,
          userNo: userNo,
          reivewDate: '',
          projectCode: '',
          projectName: '',
          contractCode: '',
          contractName: '',
          contractUnit: '',
          payNodeNo: '',
          payNodeName: '',
          buildingCodes: '',
          buildingNames: '',
          subId: '',
          subName: '',
          reviewNodeId: '',
          reviewNode: ''
        }
      }
    },
    watch: {
      'form.projectCode': function(n, o) {
        if (n !== o) {
          this.form.contractCode = ''
          this.form.contractName = ''
          this.zrhtdw.options = []
          this.form.contractUnit = ''
          this.form.payNodeNo = ''
          this.form.payNodeName = ''
          this.form.buildingCodes = ''
          this.form.buildingNames = ''
          this.htzl.options = []
          this.form.subId = ''
          this.form.subName = ''
          this.xxjd.options = []
          this.form.reviewNodeId = ''
          this.form.reviewNode = ''
        }
      }
    },
    mounted() {
      // this.ysxmSearch('')
    },
    methods: {
      // 选中形象进度节点
      mXxjdSelect(obj) {
        this.form.reviewNodeId = obj.value
        this.form.reviewNode = obj.name
        this.xxjd.show = false
      },
      // 选中验收日期
      ysrqConfirm() {
        this.form.reivewDate = moment(this.ysrqValue).format('YYYY-MM-DD')
        this.ysrqShow = false
      },
      // 查询验收项目
      ysxmSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.ysxmSearchPopup

        searchPopup.loading = true

        apiGetUserProject(userNo, val).then(res => {
          if (res.data) {
            res.data.forEach(item => {
              list.push({
                label: item.projectName,
                value: item.projectCode,
                company: item.projectCompany
              })
            })
          } else {
            this.$toast('获取项目出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确认验收项目
      ysxmConfirm(obj) {
        this.form.projectCode = obj.value
        this.form.projectName = obj.label
        this.projectCompany = obj.company
        this.ysxmShow = false
      },
      // 弹出合同查询框
      htcxMousedown() {
        if (!this.form.projectCode) {
          this.$toast('请先选择项目')
          return
        }
        this.htcxShow = true
      },
      // 查询合同
      htcxSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.htcxSearchPopup

        searchPopup.loading = true
        searchPopup.dropdownList = []

        apiGetPMContractInfo(userNo, vm.form.projectCode, vm.projectCompany, val).then(res => {
          if (res.data) {
            res.data.forEach(item => {
              list.push({
                label: item.contractName,
                value: item.contractPlanCodeLS,
                id: item.contractId,
                signerinfor: item.contractSignerinfor
              })
            })
          } else {
            this.$toast('获取合同出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确定选中合同
      htcxConfirm(obj) {
        this.form.contractCode = obj.value
        this.form.contractName = obj.label
        this.contractId = obj.id
        this.form.contractUnit = ''
        this.zrhtdw.options = []

        // JSON.parse(obj.signerinfor).signerInfor.forEach(item => {
        //   if (item.signerType != '甲方') {
        //     this.zrhtdw.options.push({
        //       name: item.companyName,
        //       value: item.companyName
        //     })
        //   }
        // })

        apiGetContractCompanyList(userNo, this.form.projectCode, obj.id, '', this.projectCompany).then(res => {
          if (res.code == 2000 && res.data.success) {
            res.data.data.forEach(item => {
              if (item.contractCompanycode != '甲方') {
                this.zrhtdw.options.push({
                  name: item.contractCompany,
                  value: item.contractCompany
                })
              }
            })
          } else {
            this.$toast('获取责任合同单位出错！')
          }
        })

        this.htcxShow = false
      },
      // 弹出责任合同选择框
      zrhtdwMousedown() {
        if (!this.form.contractCode) {
          this.$toast('请先选择合同')
          return
        }
        this.zrhtdw.show = true
      },
      // 选中责任合同单位
      zrhtdwSelect(obj) {
        this.form.contractUnit = obj.value
        this.zrhtdw.show = false
      },
      // 弹出支付节点选择框
      zfjdMousedown() {
        if (!this.form.projectCode || !this.form.contractCode) {
          this.$toast('请先选择项目和合同')
          return
        }
        this.zfjdShow = true
      },
      // 查询支付节点
      zfjdSearch(val) {
        const
          vm = this,
          list = [],
          searchPopup = vm.$refs.zfjdSearchPopup

        searchPopup.loading = true
        searchPopup.dropdownList = []

        apiGetPayNodeList(userNo, vm.form.projectCode, vm.form.contractCode, val, vm.projectCompany, vm.contractId).then(res => {
          if (res.code == 2000 && res.data.success) {
            let data = res.data.data

            data.forEach(item => {
              list.push({
                label: item.paymentNode,
                value: item.paymentNodecode,
                duixiangId: item.duixiangId
              })
            })
          } else {
            this.$toast('获取支付节点数据出错！')
          }
          searchPopup.dropdownList = list
          searchPopup.loading = false
        })
      },
      // 确定选中支付节点
      zfjdConfirm(obj) {
        const vm = this

        vm.form.payNodeNo = obj.value
        vm.form.payNodeName = obj.label
        vm.zfjdShow = false

        // 获取楼栋
        apiGetBuilingList(userNo, vm.form.projectCode, vm.form.contractCode, obj.duixiangId).then(res => {
          if (res.data) {
            let
              names = [],
              codes = []

            res.data.forEach(item => {
              names.push(item.DUIXIANGMINGCHENG)
              codes.push(item.DUIXIANGID)
            })

            vm.form.buildingNames = names.join(',')
            vm.form.buildingCodes = codes.join(',')
          } else {
            vm.$toast('获取楼栋数据出错！')
          }
        })

        // 获取分包合同种类
        apiGetSubpackageList(userNo, vm.form.contractCode).then(res => {
          if (res.data) {
            let
              fbOps = [],
              jdOps = []

            res.data.forEach(item => {
              fbOps.push({
                name: item.SUBNAME,
                value: item.SUBID
              })
              jdOps.push({
                name: item.NODENAME,
                value: item.NODEID
              })
            })

            vm.htzl.options = fbOps
            vm.xxjd.options = jdOps
          } else {
            vm.$toast('获取分包合同种类出错！')
          }
        })
      },
      // 弹出分包合同选择框
      htzlMousedown() {
        if (!this.form.payNodeNo) {
          this.$toast('请先选择支付节点')
          return
        }
        this.htzl.show = true
      },
      // 选中分包合同
      htzlSelect(obj) {
        this.form.subId = obj.value
        this.form.subName = obj.name
        this.htzl.show = false
      },
      // 弹出形象进度节点选择框
      xxjdMousedown() {
        if (!this.form.subId) {
          this.$toast('请先选择分包合同种类')
          return
        }
        this.xxjd.show = true
      },
      // 提交数据
      submitForm() {
        if (!this.form.reviewNodeId || !this.form.reivewDate) {
          this.$toast('信息填写不完整！')
          return
        }

        apiPostBaseInfo(userNo, this.form).then(res => {
          if (res.data) {
            localStorage.setItem('addedID', res.data)
            this.$router.push('/engNodeAcpt/addremain')
          } else {
            this.$toast('新增失败，请重试！')
          }
        })
      }
    }
  }
</script>